Skip to content

perf(runtime): drop three emulated thread_locals from hot paths - #2137

Draft
edusperoni wants to merge 3 commits into
mainfrom
perf/runtime-scratch-tls
Draft

edusperoni wants to merge 3 commits into
mainfrom
perf/runtime-scratch-tls

Conversation

@edusperoni

@edusperoni edusperoni commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Description

The runtime's minSdk is below 29, so every thread_local it declares is emulated: each access is a call to __emutls_get_address, and a thread_local with a destructor also has to look up __tls_guard (or its own init guard) first. This PR removes three of them from hot paths. In each case only one thread touches the data: the thread holding the isolate's Locker, or the event loop's home thread. So it can live on the object instead.

  1. MethodCache's signature key (static thread_local std::string s_signatureKey) becomes a per-Runtime scratch string, reached through Runtime::GetRuntime(args.GetIsolate())->GetSignatureKeyScratch(). On a cache hit, ResolveMethodSignature and ResolveConstructorSignature each made three __emutls_get_address calls: guard, string, guard. That cost was paid on every overload-resolved JS→Java call and every Java construction from JS (e.g. new java.lang.Object()). The key is still copied before resolving, because resolving calls into Java, which can call back into JS and reuse the scratch.
  2. SetDefaultOverrides' field cache (a function-local thread_local robin_hood::unordered_map<jclass, jfieldID>) moves onto the Runtime. This removes two lookups (the map and its init guard) from every construction of an implementation with default-method overrides, plus the per-thread TLS destructor registration.
  3. EventLoop's looper callback depth (thread_local int t_looperCallbackDepth) becomes an EventLoop member. This removes one lookup per eventfd/timerfd callback. The fd callbacks already receive the EventLoop as their data pointer and only run on its home thread. As a result, IsInLooperCallback() changes from a static to an instance method that answers for that loop's home thread. It has no callers today.

Why a per-Runtime value is safe: a runtime's JS, and so MethodCache and RegisterInstance, only runs on the thread holding its isolate's Locker, so two threads never share the scratch at once. Same-thread re-entry (Java calling back into JS mid-resolve) is handled by the copy noted in (1). Same-thread re-entry already existed with the thread_local.

Emulated-TLS call sites

bl __emutls_get_address sites per control variable in the linked, unstripped arm64-v8a libNativeScript.so (:runtime:externalNativeBuildRelease, RelWithDebInfo, NDK 27.3):

variable before after
MethodCache s_signatureKey 8 0
__tls_guard (all) 14 2 (V8's)
SetDefaultOverrides map + its init guard 4 0
EventLoop t_looperCallbackDepth 2 0
whole library, all variables 1627 1595

The four rows account for 26 of the 32 removed sites. The other 6 come from code-generation and inlining differences elsewhere in the library, which were not attributed to a variable. In the three changed objects (MethodCache.cpp.o, CallbackHandlers.cpp.o, EventLoop.cpp.o), the relocations go from 68 to 41 calls. That is 27 rather than 26 because one t_looperCallbackDepth site was in IsInLooperCallback(), which has no callers and is dropped at link. The only emutls variables left in those objects are JEnv::s_thread and, in EventLoop.cpp.o, t_pumpDepth.

No benchmark numbers: each removed lookup is a few ns, and the counts above are the claim.

Testing

Full suite (runtestsAndVerifyResults) on an arm64 API 36.1 emulator: 1773 specs, 0 failures.

MethodCache encoded every overload-resolved call's and constructor's cache
key into a `static thread_local std::string`. The runtime's minSdk is below
29, so that thread_local is emulated: every access goes through
__emutls_get_address, and because the string has a destructor, through a
second lookup of the TU's __tls_guard as well. A cache hit made three such
calls, on every overload-resolved JS->Java call and every Java
construction from JS (e.g. `new java.lang.Object()`).

The key is only ever built by the thread holding the isolate's Locker,
so a scratch string owned by the Runtime serves the same purpose without
TLS. The key is still copied before resolving, because resolving calls
into Java, which can call back into JS and reuse the scratch.
SetDefaultOverrides cached each class's defaultOverrides__ns field in a
function-local thread_local map. Below API 29 thread_locals are emulated,
so every construction of an implementation with default-method overrides
paid two __emutls_get_address lookups (the map and its thread_local
initialization guard), and each thread that touched it registered a TLS
destructor.

Only the thread holding the isolate's Locker constructs instances, so the
cache moves onto the Runtime.
Every eventfd/timerfd ALooper callback incremented and decremented a
thread_local depth counter. Below API 29 that thread_local is emulated,
so each callback paid an __emutls_get_address lookup.

The fd callbacks only run on the loop's home thread and already receive
the EventLoop as their data pointer, so the depth becomes an EventLoop
member and IsInLooperCallback() an instance method answering for that
loop's home thread. It currently has no callers.
@coderabbitai

coderabbitai Bot commented Nov 21, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant